package org.unt.genlogic;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import org.apache.log4j.Logger;

public class Zips {

	private static Logger log = Logger.getLogger(Zips.class.getCanonicalName());

	/**
	 * Builds a Map <ZipEntry.getName(), new Long(ZipEntry.getSize())>
	 * 
	 * @param zipFile
	 * @return
	 */
	public static Map getZipEntriesAsMap(ZipFile zipFile) {

		Map map = new HashMap();

		getZipEntriesAsMap(zipFile, map);

		return map;
	}

	/**
	 * Fills a Map <ZipEntry.getName(), new Long(ZipEntry.getSize())>
	 * 
	 * @param zipFile
	 * @param map
	 * @return
	 */
	public static void getZipEntriesAsMap(ZipFile zipFile, Map map) {

		Enumeration enumeration = zipFile.entries();

		while (enumeration.hasMoreElements()) {

			ZipEntry entry = (ZipEntry) enumeration.nextElement();

			if (entry.isDirectory()) {
				continue;
			}

			if (map.containsKey(entry.getName())) {
				log.warn("Entry already loaded in map was overwritten : " + entry.getName());
			}

			map.put(entry.getName(), new Long(entry.getSize()));
		}
	}
}
